package com.leyou.trade.controller;


import com.leyou.trade.entity.CartItem;
import com.leyou.trade.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：qcq
 * @date ：Created in 2020/12/21 11:50
 * @description：
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param cartItem
     * @return
     */
    @PostMapping
    public ResponseEntity addCart(@RequestBody CartItem cartItem) {
        cartService.addCart(cartItem);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询并显示购物车
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CartItem>> queryCartList() {
        return ResponseEntity.ok(cartService.queryCartList());
    }

    /**
     * 修改购物车中的商品数量
     * @param id
     * @param num
     * @return
     */
    @PutMapping
    public ResponseEntity updateCart(@RequestParam("id") Long id, @RequestParam("num") Integer num) {
        cartService.updateCart(id, num);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{skuId}")
    public ResponseEntity deleteCart(@PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();
    }



}
